From 2a66dfba1f29cabf434ccdb73738ed9b46a00716 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 1 Aug 2016 13:28:25 -0700 Subject: [PATCH] Use openssl on Unix rather than C functions directly for SHA256 --- Cargo.lock | 34 +++++++++++++++++++++++++ Cargo.toml | 3 +++ src/cargo/util/sha256.rs | 55 +++++++--------------------------------- 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc6fcb967..93c055af5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,7 @@ dependencies = [ "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "miow 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -56,6 +57,11 @@ name = "bitflags" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bitflags" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bufstream" version = "0.1.1" @@ -244,6 +250,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lazy_static" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "0.2.1" @@ -368,6 +379,19 @@ dependencies = [ "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "openssl" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys-extras 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "openssl-sys" version = "0.7.8" @@ -380,6 +404,16 @@ dependencies = [ "user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "openssl-sys-extras" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pkg-config" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index c731a81e2..db6c8d6df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,9 @@ toml = "0.1.29" url = "1.1" winapi = "0.2" +[target.'cfg(unix)'.dependencies] +openssl = "0.7" + [dev-dependencies] hamcrest = "0.1" bufstream = "0.1" diff --git a/src/cargo/util/sha256.rs b/src/cargo/util/sha256.rs index 18a258af3..97bee1c35 100644 --- a/src/cargo/util/sha256.rs +++ b/src/cargo/util/sha256.rs @@ -6,63 +6,26 @@ pub use self::imp::Sha256; // allow improper ctypes because size_t falls under that in old compilers #[allow(bad_style, improper_ctypes)] mod imp { - use libc; + extern crate openssl; - enum EVP_MD_CTX {} - enum EVP_MD {} - enum ENGINE {} + use std::io::Write; + use self::openssl::crypto::hash::{Hasher, Type}; - extern { - fn EVP_DigestInit_ex(ctx: *mut EVP_MD_CTX, - kind: *const EVP_MD, - imp: *mut ENGINE) -> libc::c_int; - fn EVP_DigestUpdate(ctx: *mut EVP_MD_CTX, - d: *const libc::c_void, - cnt: libc::size_t) -> libc::c_int; - fn EVP_DigestFinal_ex(ctx: *mut EVP_MD_CTX, md: *mut libc::c_uchar, - s: *mut libc::c_uint) -> libc::c_int; - fn EVP_MD_CTX_create() -> *mut EVP_MD_CTX; - fn EVP_MD_CTX_destroy(ctx: *mut EVP_MD_CTX); - fn EVP_sha256() -> *const EVP_MD; - } - - pub struct Sha256 { ctx: *mut EVP_MD_CTX } + pub struct Sha256(Hasher); impl Sha256 { pub fn new() -> Sha256 { - unsafe { - let ctx = EVP_MD_CTX_create(); - assert!(!ctx.is_null()); - let ret = Sha256 { ctx: ctx }; - let n = EVP_DigestInit_ex(ret.ctx, EVP_sha256(), 0 as *mut _); - assert_eq!(n, 1); - ret - } + Sha256(Hasher::new(Type::SHA256)) } pub fn update(&mut self, bytes: &[u8]) { - unsafe { - let n = EVP_DigestUpdate(self.ctx, bytes.as_ptr() as *const _, - bytes.len() as libc::size_t); - assert_eq!(n, 1); - } + let _ = self.0.write_all(bytes); } pub fn finish(&mut self) -> [u8; 32] { - unsafe { - let mut ret = [0u8; 32]; - let mut out = 0; - let n = EVP_DigestFinal_ex(self.ctx, ret.as_mut_ptr(), &mut out); - assert_eq!(n, 1); - assert_eq!(out, 32); - ret - } - } - } - - impl Drop for Sha256 { - fn drop(&mut self) { - unsafe { EVP_MD_CTX_destroy(self.ctx) } + let mut ret = [0u8; 32]; + ret.copy_from_slice(&self.0.finish()[..]); + ret } } } -- 2.30.2